#这个项目用来将原来项目的结构拆分成主流程+多个类的模式，便于更改和调试
#启动时间2025年3月1日
#修复了资源页面升级的bug，具体原因不知道，但是增加了判断，含有good，不含notnow,maxlevel字样更加精准
#修改了倒计时不显示问题，print(f"{i}", end=" ") end=" "中不能添加\r

from LoginAutoTimer import LoginAutoTimer
import os
import sys
import ast


def parse_config(filename):
    config = {}
    with open(filename, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    current_key = None
    list_buffer = []
    dict_buffer = []
    in_list = False
    in_dict = False

    for line in lines:
        line = line.strip()

        if not line:
            continue

        if '=' in line and not in_list and not in_dict:
            key, value = map(str.strip, line.split('=', 1))
            if value.startswith('['):
                current_key = key
                list_buffer = [value]
                in_list = True
                if value.endswith(']'):
                    in_list = False
                    list_str = ' '.join(list_buffer).replace('\\', '')
                    config[current_key] = ast.literal_eval(list_str)
            elif value.startswith('{'):
                current_key = key
                dict_buffer = [value]
                in_dict = True
                if value.endswith('}'):
                    in_dict = False
                    dict_str = ' '.join(dict_buffer).replace('\\', '')
                    config[current_key] = ast.literal_eval(dict_str)
            else:
                if value.lower() in ('true', 'false'):
                    config[key] = value.lower() == 'true'
                elif value.isdigit():
                    config[key] = int(value)
                else:
                    config[key] = value.strip('"')
        elif in_list:
            list_buffer.append(line)
            if line.endswith(']'):
                in_list = False
                list_str = ' '.join(list_buffer).replace('\\', '')
                config[current_key] = ast.literal_eval(list_str)
        elif in_dict:
            dict_buffer.append(line)
            if line.endswith('}'):
                in_dict = False
                dict_str = ' '.join(dict_buffer).replace('\\', '')
                config[current_key] = ast.literal_eval(dict_str)
    return config


def get_config_path():
    # 获取当前执行文件的路径
    current_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    # 构造config.txt文件的路径
    config_path = os.path.join(current_dir, "config.txt")
    return config_path


config_path = get_config_path()
config = parse_config(config_path)
interval = config['interval']
max_runs = config['max_runs']
game_login_url = config['game_login_url']
username = config['username']
password = config['password']
village_urls = config['village_urls']
building_urls = config['building_urls']
build_sequence = config['build_sequence']

print(f"每次运行后等待时间: {interval}秒钟")
print(f"本次操作运行次数: {max_runs}")
print(f"登录地址: {game_login_url}")
print(f"账号: {username}")
print(f"密码: {'*' * len(str(password))}")
print(f"村庄资源页数量: {len(village_urls)}")
print(f"建筑页面数量: {len(building_urls)}")
print(f"建筑序列条目: {build_sequence}")
print(f"文件路径为：{config_path}")




if __name__ == "__main__":
    config_path = get_config_path()
    config = parse_config(config_path)
    game_login_url = config.get("game_login_url")
    username = config.get("username")
    password = config.get("password")
    village_urls = config.get("village_urls")
    building_urls = config.get("building_urls")
    build_sequence = config.get("build_sequence")
    interval = config.get("interval", 300)
    max_runs = config.get("max_runs", 50)

    timer = LoginAutoTimer(game_login_url, username, password, village_urls, building_urls, build_sequence, interval, max_runs)
    timer.start()